<template>
  <!-- 搜索 -->
  <div class="my-wbox">
    <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="120px" size="large">
      <el-form-item label="旧密码" prop="oldPwd">
        <el-input v-model="ruleForm.oldPwd" type="password" show-password :disabled="loading" />
      </el-form-item>
      <el-form-item label="新密码" prop="newPwd">
        <el-input v-model="ruleForm.newPwd" type="password" show-password :disabled="loading" />
      </el-form-item>
      <el-form-item label="确认密码" prop="repeatNewPwd">
        <el-input v-model="ruleForm.repeatNewPwd" show-password :disabled="loading" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading">确定</el-button>
        <el-button @click="resetForm()" :loading="loading">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup lang='ts'>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance } from 'element-plus'
import { useRouter } from 'vue-router'
import { updatePwdApi } from '@/views/account/api/accountApi'
import { UpdatePwdParams } from '@/views/account/interface/interface'
import { Md5 } from 'ts-md5/dist/md5';
import { cleanSession } from '@/global/session'
const router = useRouter()
const ruleFormRef = ref<FormInstance>()
const loading=ref<boolean>(false)
// 新密码校验
const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入新密码'))
  } else {
    if (ruleForm.repeatNewPwd !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('repeatNewPwd', () => null)
    }
    callback()
  }
}
// 确认密码校验
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入新密码'))
  } else if (value !== ruleForm.newPwd) {
    callback(new Error("两次密码不一样!"))
  } else {
    callback()
  }
}
//修改密码表单
const ruleForm = reactive({
  oldPwd: '',
  newPwd: '',
  repeatNewPwd: ''
})
//表单校验规则
const rules = reactive({
  oldPwd: [{ required: true, message: '请输入旧密码', trigger: 'blur' }],
  newPwd: [{ required: true,validator: validatePass, trigger: 'blur' }],
  repeatNewPwd: [{ required: true,validator: validatePass2, trigger: 'blur' }],
})
//确定
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  loading.value=true
  formEl.validate((valid) => {
    if (valid) {
      let params: UpdatePwdParams={
        oldPwd: Md5.hashStr(ruleForm.oldPwd),
        newPwd: Md5.hashStr(ruleForm.newPwd),
        repeatNewPwd: Md5.hashStr(ruleForm.repeatNewPwd),
      }
      onUpdatePwd(params)
    } else {
      loading.value=false
      return false
    }
  })
}
//取消
const resetForm = () => {
  router.go(-1)
}
//修改密码
const onUpdatePwd = async (params: UpdatePwdParams) => {
  try {
    let res = await updatePwdApi(params)
    if (res?.code === 0) {
      // router.go(-1)
      ElMessage({ type: 'success', message: '修改成功' })
      cleanSession()
      router.replace({ name: 'login' })
    }
    loading.value=false
  } catch {
    loading.value=false
  }
}
</script>
<style scoped lang='scss'>
</style>